package com.hylc.biz.ui.activity;

import static com.xls.commonlibrary.arouter.path.APath.BIZ_SPLASH_ACTIVITY;

import android.content.Intent;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.hylc.biz.R;
import com.orhanobut.logger.Logger;
import com.xls.boxlibrary.utils.AppUtils;
import com.xls.commonlibrary.ad.adn.gm.GMSplashUtil;
import com.xls.commonlibrary.base.BaseActivity;
import com.xls.commonlibrary.http.RxLifecycle;
import com.xls.commonlibrary.http.Transformer;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

@Route(path = BIZ_SPLASH_ACTIVITY)
public class SplashActivity extends BaseActivity {
    private static final String TAG = "SplashActivity";
    private FrameLayout mSplashContainer;// 闪屏广告布局
    ImageView iv_logo;
    ProgressBar img_show;
    // 延迟秒数
    int delay = 3500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mSplashContainer = findViewById(R.id.splash_container);
        img_show=findViewById(R.id.img_show);
        iv_logo=findViewById(R.id.iv_logo);
        iv_logo.setImageBitmap(AppUtils.getBitmap(this));
        loadSplash();
//        delay();
    }

    void delay() {

        Observable.just(1)
                .delay(delay, TimeUnit.SECONDS)
                .compose(Transformer.transform())
                .compose(RxLifecycle.bindRxLifecycle(this))
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Logger.d("========SplashActivity=========onSubscribe======");
                    }

                    @Override
                    public void onNext(@NonNull Integer integer) {

                        Logger.d("========SplashActivity=========onNext======");
                        // 延迟 加载广告
                        loadSplash();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Logger.d("========SplashActivity=========onError======");
                    }

                    @Override
                    public void onComplete() {
                        Logger.d("========SplashActivity=========onComplete======");
                    }
                });
    }

    void loadSplash() {
        // todo 需要缓存一个开屏广告或者倒计时，等sdk init ok 初始化成功才可以调广告

        if (TTAdSdk.isInitSuccess()) {
            Logger.d("========SplashActivity=========isInitSuccess====true==");
            GMSplashUtil.getInstance().loadSplash(this, mSplashContainer, result -> {
                // hook
                goToMainActivity();
            },delay);

            // tx splash
//            TxSplashUtil.getInstance().loadSplash(this, mSplashContainer, result -> {
//                goToMainActivity();
//            });

            // chage to sigmob splash
//            SigmobSplashUtil.getInstance().loadSplash(this, mSplashContainer, result -> {
//                goToMainActivity();
//            });


        } else {
            Logger.d("========SplashActivity=========isInitSuccess====false==");
            goToMainActivity();
        }
    }


    /**
     * 跳转到主页面
     */
    private void goToMainActivity() {

        Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
        startActivity(intent);
        mSplashContainer.removeAllViews();
        this.finish();
    }

}
